package com.google.code.mergeinformer.model.security;

import com.google.code.mergeinformer.model.DefaultEntity;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.Date;

/**
 * @author Dmitry Golubev
 */
@NamedQueries({
        @NamedQuery(name = "RememberMeToken.findBySeries", query = "SELECT rmt FROM RememberMeToken rmt WHERE rmt.series = :series"),
        @NamedQuery(name = "RememberMeToken.removeUserTokens", query = "DELETE FROM RememberMeToken rmt WHERE rmt.user = (SELECT u FROM User u where u.username = :username)")
})
@Entity
@Table(name = "rememberme_token")
public class RememberMeToken extends DefaultEntity {

    @NotEmpty
    @Size(max = 32)
    @Column(name = "series", nullable = false, unique = true)
    private String series;

    @NotEmpty
    @Size(max = 32)
    @Column(name = "token", nullable = false)
    private String token;

    @Column(name = "last_used")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastUsed;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "id_app_user", nullable = false)
    private User user;

    public String getSeries() {
        return series;
    }

    public void setSeries(String series) {
        this.series = series;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Date getLastUsed() {
        return lastUsed;
    }

    public void setLastUsed(Date lastUsed) {
        this.lastUsed = lastUsed;
    }

    public User getUser() {
        return user;
    }

    void setUser(User user) {
        this.user = user;
    }
}
